import tkinter as tk
from src.utils.loguru_func import app_init_logger
from src.utils.modbus_func import Huikong_Output
from src.utils.state_machine import Step_Manager
from src.layout import Main_Window
import json


class APP:
    def __init__(self, root: tk.Tk):
        with open('config/IO_485_COM.json', mode='r', encoding='utf-8') as f:
            data = json.load(f)
            self.IO_485_COM = data['COM_485']

        app_init_logger.debug("APP初始化")

        self.ROOT = root
        self.HUIKONG_OUTPUT = Huikong_Output(self.IO_485_COM)
        app_init_logger.debug("HUIKONG_OUTPUT始化完成")
        self.set_mainwindow()

        self.set_step_manager()

    def set_mainwindow(self):

        self.MAIN_WINDOW = Main_Window(self.ROOT, self)

        app_init_logger.debug("主窗口初始化完成")
        self.MAIN_WINDOW.APP = self
        self.ROOT.withdraw()

    def set_step_manager(self):
        self.STEP_MANAGER = Step_Manager(self)
        app_init_logger.debug("step manager 初始化完成")
        # self.STEP_MANAGER.step_start()

    def main_loop(self):
        self.ROOT.mainloop()
